package com.kf.smartpark.main

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.webkit.JavascriptInterface
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.BarUtils
import com.bw.x5.WebViewJavaScriptFunction
import com.kf.smartpark.MainWebAIDL
import com.kf.smartpark.R
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import kotlinx.coroutines.DEBUG_PROPERTY_VALUE_AUTO

@Route(path = "/main/mainactivity")
class MainActivity : AppCompatActivity() {

    private lateinit var webview: WebView
    private lateinit var webSettings: WebSettings
    private lateinit var aidl: MainWebAIDL

    @Autowired
    lateinit var loadUrl: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ARouter.getInstance().inject(this)
        BarUtils.setStatusBarColor(this, Color.parseColor("#2196F3"))
        initBindService()
        initView()
    }

    private fun initBindService() {
        var intent = Intent()
        intent.action = "com.kf.smartpark.main.MainWebService"
        intent.`package` = "com.kf.smartpark"
        bindService(intent, connection, BIND_AUTO_CREATE)
    }

    override fun onDestroy() {
        if (webview != null)
            webview.destroy()
        unbindService(connection)
        super.onDestroy()
    }

    private fun initView() {
        webview = findViewById(R.id.main_webView)
        webSettings = webview.settings
        webSettings.setJavaScriptEnabled(true)
        webSettings.setAllowFileAccess(true)
        webSettings.setSupportZoom(true)
        webSettings.setDatabaseEnabled(true)
        webSettings.setAllowFileAccess(true)
        webSettings.setDomStorageEnabled(true)
        webview.addJavascriptInterface(object : WebViewJavaScriptFunction {
            override fun onJsFunctionCalled(tag: String) {

            }

            //车位申请
            @JavascriptInterface
            fun androidparking(action: String) {
                Log.e("HXW", "androidparking被点击")
                aidl.setWebName("androidparking")
            }

            //我的申请
            @JavascriptInterface
            fun androidapply(action: String) {
                Log.e("HXW", "androidapply被点击")
                aidl.setWebName("androidapply")
            }

            //来访预约
            @JavascriptInterface
            fun androidvisit(action: String) {
                Log.e("HXW", "androidvisit被点击")
                aidl.setWebName("androidvisit")
            }

            //巡更管理
            @JavascriptInterface
            fun androidpatrol(action: String) {
                Log.e("HXW", "androidpatrol被点击")
                aidl.setWebName("androidpatrol")
            }

            //维修管理
            @JavascriptInterface
            fun androidrepair(action: String) {
                Log.e("HXW", "androidrepair被点击")
                aidl.setWebName("androidrepair")
            }

            //添加维修
            @JavascriptInterface
            fun androidaddrepair(action: String) {
                Log.e("HXW", "androidaddrepair被点击")
                aidl.setWebName("androidaddrepair")
            }

            //文化编辑
            @JavascriptInterface
            fun androidaddculture(action: String) {
                Log.e("HXW", "androidaddculture被点击")
                aidl.setWebName("androidaddculture")
            }

            //公告管理
            @JavascriptInterface
            fun androidaddnotice(action: String) {
                Log.e("HXW", "androidaddnotice被点击")
                aidl.setWebName("androidaddnotice")
            }

            //文化管理
            @JavascriptInterface
            fun androidculture(action: String) {
                Log.e("HXW", "androidculture被点击")
                aidl.setWebName("androidculture")
            }

            //公告管理
            @JavascriptInterface
            fun androidnotice(action: String) {
                Log.e("HXW", "androidnotice被点击")
                aidl.setWebName("androidnotice")
            }

            //签到
            @JavascriptInterface
            fun androidsign(action: String) {
                Log.e("HXW", "androidsign被点击")
                aidl.setWebName("androidsign")
            }

            //考勤管理
            @JavascriptInterface
            fun androidattendance(action: String) {
                Log.e("HXW", "androidattendance被点击")
                aidl.setWebName("androidattendance")
            }

            //物业审核
            @JavascriptInterface
            fun androidproperty(action: String) {
                Log.e("HXW", "androidproperty被点击")
                aidl.setWebName("androidproperty")
            }

            //文化审核
            @JavascriptInterface
            fun androidcheckculture(action: String) {
                Log.e("HXW", "androidcheckculture被点击")
                aidl.setWebName("androidcheckculture")
            }

            //人员管理
            @JavascriptInterface
            fun androidpeople(action: String) {
                Log.e("HXW", "androidpeople被点击")
                aidl.setWebName("androidpeople")
            }
        }, "androidinfo")
        webview.loadUrl(loadUrl)
    }

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            aidl = MainWebAIDL.Stub.asInterface(service)
        }

        override fun onServiceDisconnected(name: ComponentName?) {

        }

    }
}